function ProtectionStatusMan(){
	var pieNums = {
			all:0,
			alarm:0
	};
	var alarmStations = [];
	var alarms = [];
	var divId = 'affectObjDiv';
	var pieId = 'protectionPie';
	var rightDivTitle = '保护装置工作状态';
	var dailyUtil = new DailyUtil();
	var popMap = {
			device:new Map()
	};
	var currentTime;
	var currentPop = undefined;
	var piechart = undefined;
	var start = undefined;
	var end = undefined;
	this.show = function(){
		initData();
	};
	this.onSelectPoint = function(value){
		var pop = popMap.device.get(value);
		if(pop){
			if(pop.isShow()){
				pop.remove();
			}else{
				pop.show();
			}
		}
	};
	this.hide = function(){
		$('#'+divId).css('display','none');
	};
	this.refreshData = function(){
		initData();
	};
	function initData(){
		clearAll();
		$.ajaxSettings.async = false;
		currentTime = getSecondTimeStr(new Date().getTime());
		$.getJSON('/rest/protectAlarm/getProtectionDeviceStatusByTime/'+currentTime,function(result){
			pieNums.all = result.allNum;
			pieNums.alarm = result.alarmProtectionNum;
			alarmStations = result.stations;
			alarms = result.alarms;
			show();
		});
		$.ajaxSettings.async = true;
	}
	function getDayTimeStr(millis){
		if (!millis) {
			return "";
		}
		var t = new Date(millis);
		var m = t.getMonth() + 1 > 9 ? (t.getMonth() + 1) : ("0" + (t
				.getMonth() + 1));
		var d = t.getDate() > 9 ? t.getDate() : "0" + t.getDate();
		var timestr = t.getFullYear() + "-" + m + "-" + d;
		return timestr;
	}
	function getSecondTimeStr(millis){
		if (!millis) {
			return "";
		}
		var t = new Date(millis);
		var m = t.getMonth() + 1 > 9 ? (t.getMonth() + 1) : ("0" + (t
				.getMonth() + 1));
		var d = t.getDate() > 9 ? t.getDate() : "0" + t.getDate();
		var h = t.getHours() > 9 ? t.getHours() : "0" + t.getHours();
		var min = t.getMinutes() > 9 ? t.getMinutes() : "0" + t.getMinutes();
		var s = t.getSeconds() > 9 ? t.getSeconds() : "0" + t.getSeconds();
		var timestr = t.getFullYear() + "-" + m + "-" + d+" "+h+":"+min+":"+s;
		return timestr;
	}
	function sortAlarm(obj1,obj2){
		var t1 = obj1.eventStart?obj1.eventStart:0;
		t1 = t1.time?t1.time:t1;
		var t2 = obj2.eventStart?obj2.eventStart:0;
		t2 = t2.time?t2.time:t2;
		return t2-t1;
	}
	function show(){
		clearAll();
		selectedLayer.setVisibility(true);
		drawStationPoints();
		drawRightDiv();
		addDevicePops();
		if(piechart){
			piechart.resize();
		}
	}
	function drawRightDiv(){
		var panel = new PanelTablesUtil(divId);
		panel.setTitle(rightDivTitle).setTitleTool('<i class="fa fa-refresh"></i>', "protectionStatusMan.refreshData();", 'refreshBtn');
		panel.setSuccessBox();
		//drawAnimatorDiv(panel);
		drawPie(panel);
		drawDeviceTable(panel);
		panel.show();
	}
	function markPoint(map, gif, type, width, height) {
		var gwidth = width ? width : 35;
		var gheight = height ? height : 35;
		var heatPoints = [];
		map.each(function(index, key, value) {
			var lonlatName = key.split(',');
			var point = new SuperMap.Geometry.Point(lonlatName[0],
					lonlatName[1]);
			var gifPoint = new SuperMap.Feature.Vector(point, null, {
				externalGraphic : gif,
				graphicWidth : gwidth,
				graphicHeight : gheight,
				graphicOpacity : 1,
			});
			gifPoint.data.type = type;
			gifPoint.data.value = value;
			heatPoints.push(gifPoint);
		});
		selectedLayer.addFeatures(heatPoints);
		selectedLayer.setVisibility(true);
		selectFeature.activate();
	}
	this.openDialog = function(value,type){
		var url = '';
		var params = '';
		if(type == 'protectionStatusDevice'){
			url = "/ui/map/dialog/daily/dailyDeviceAlarm.xhtml?";
			params = 'time='+currentTime;
		}
		MyModal.showModal(url + params);
	};
	this.openPopDialog = function(value){
		url = "/ui/map/dialog/daily/dailyDeviceAlarm.xhtml?";
		params = 'time='+currentTime+'&stationId='+value;
		MyModal.showModal(url + params);
	};
	/*this.playBack = function(start,end){
		start = getSecondTimeStr(new Date(1504249200000));
		end = getSecondTimeStr(new Date(1504252800000));
		$.getJSON("/rest/protectAlarm/getProtectAlarmsByStartAndEnd/"+start+"/"+end,function(result){
			//开始时间或复归时间
			//[id,lon,lat,time]
			playBack(result);
		});
	};*/
	function playBack(data){
		var datas = [];
		for(var i in data){
			var d = data[i];
			var s = d.eventStart.time?d.eventStart.time+d.mStartMs:d.eventStart+d.mStartMs;
			var e ;
			if(d.revertTime){
				e = d.revertTime.time?d.revertTime.time+d.mReverMs:d.revertTime+d.mReverMs;
			}else{
				e = 1504252800000;
			}
			var da = {
					id:d.id,
					startTime:s,
					endTime:e,
					lon:d.lon,
					lat:d.lat,
					data:d
			};
			datas.push(da);
		}
		var ani = new RealTimeAnimator();
		ani.setStartAndEndMsec(1504249200000,1504252800000);
		ani.setDatas(datas);
		ani.start();
	}
	function drawAnimatorDiv(panel){
		panel.setInfo('<span class="btn" onclick="protectionStatusMan.playBack();">回放</span>');
	}
	function getAlarmDataset(){
		var datas = [];
		for(var i in alarms){
			var a = alarms[i];
			datas.push([str(a.stationName),getSecondTimeStr(a.eventStart.time?a.eventStart.time:a.eventStart),str(a.alarmMsg)]);
		}
		return datas;
	}
	function str(v){
		return v?v:'';
	}
	function drawDeviceTable(panel){
		var expand = false;
		var title = "保护装置告警";
		var detail = "<span style='color:gray;' onclick='protectionStatusMan.openDialog(null,\"protectionStatusDevice\")' class='pull-right'>明细</span>";
		if (alarms.length > 0) {
			title += "<span title='保护装置告警个数' class='badge bg-red pull-right' onclick='protectionStatusMan.openDialog(null,\'protectionStatusDevice\')'>"+alarms.length+"</span>";
			title += detail;
			panel.drawCollapseTable("protectDeviceTable",title,expand);
			var columns = ["厂站名称","告警时间","告警内容"];
			var table = new MyTable("protectDeviceTable",columns,dailyUtil.getTableOption());
			table.setDataSet(getAlarmDataset());
			table.show();
			table.onRowClick(function(data) {
				if (data) {
					$("#input_search").val(data[0]);
					searchObj();
				}
			});
		} else {
			var ele = panel.getCollapsePanelEle("protectDeviceTable",title+detail,expand);
			ele.append(PanelTablesUtil.getSmileInfo("无保护装置告警"));
		}
	}
	function drawPie(panel){
		var ele = panel.getCollapsePanelEle(pieId+'Div','保护状态',true);
		ele.append('<div id="'+pieId+'" style="height:250px;width:300px" ></div>');
		piechart = echarts.init(document.getElementById(pieId));
		var timeOption = {
			color:['green','red'],
			legend:{
				data:["主保护正常","主保护异常"]
			},
			series:[{
		name:'主保护状态',
		type:'pie',
		radius:['30%','50%'],
		minAngle:1,
		startAngle:0,
		label: {
			normal: {
				fontSize :18,
				formatter:'{b}\n{c}个'
			}
		},
		data:getPieData()
	}]
	};
	piechart.setOption(timeOption);
		
	}
	function getPieData(){
		var pieData = [];
		pieData.push({name:"主保护正常",value:pieNums.all-pieNums.alarm});
		pieData.push({name:"主保护异常",value:pieNums.alarm});
		return pieData;
	}
	function drawStationPoints(){
		var features = [];
		for(var item in alarmStations){
			var count = alarmStations[item];
			var ss = item.split(",");
			features.push(getPoint(ss[0],ss[1],ss[2],count));
			features.push(getNamePoint(ss[0],ss[1],ss[2],ss[3]));
		}
		selectFeature.activate();
		selectedLayer.addFeatures(features);
		selectedLayer.setVisibility(true);
	}
	function getPoint(lon,lat,stationId,count){
		var point = new SuperMap.Geometry.Point(lon, lat);
		var pointVector = new SuperMap.Feature.Vector(point);
		pointVector.style = {
			fillColor : "red",
			strokeColor : "yellow",
			pointRadius : 15,
			label: count+"",
			labelXOffset:'-3',
			fontColor:"white",
			fontOpacity:"0.8",
			fontFamily:"隶书",
			fontSize:"15px",
			fontWeight:"bold",
			fontStyle:"italic",
			labelSelect:"true"
		};
		pointVector.data.type = "protectionStatusStation";
		pointVector.data.value= stationId;
		return pointVector;
	}
	function getNamePoint(lon,lat,stationId,stationName){
		var point = new SuperMap.Geometry.Point(lon, lat);
	//	var point = new SuperMap.Feature.ShapeParameters.Label(station.longitude,station.latitude,station.name);
		var pointVector = new SuperMap.Feature.Vector(point);
		pointVector.style = {
				fillColor : "red",
				strokeColor : "yellow",
				pointRadius : 0,
				label:stationName+"",
				labelXOffset:'-3',
				labelYOffset:'-25',
				fontColor:"black",
				fontOpacity:"1",
				fontSize:"15px",
				fontWeight:"bold",
				labelSelect:"true"
		};
		pointVector.data.type = "protectionStatusStation";
		pointVector.data.value= stationId;
		return pointVector;
	}
	function addDevicePops() { 
		var devicemap = new Map(); 
		for (var i in alarms) {
			var r = alarms[i]; 
			var ds = devicemap.get(r['stationId']); 
			if (!ds) { 
				ds = [];
				devicemap.put(r['stationId'], ds); 
			} 
			ds.push(r); 
		}
		popMap.device.clear();
		devicemap.each(function(index, key, value) {
			var lon = 0;
			var lat = 0;
			for(var i in alarmStations){
				if(i.indexOf(value[0].stationName)>-1){
					var s = i.split(",");
					lon = s[0];
					lat = s[1];
				}
			}
			var pop = getDevicePop(lon,lat,value);
			popMap.device.put(key, pop); 
	//		pop.show(); 
		}); 
	}
	function getDevicePop(lon,lat,value){
		var clicklistener = 'protectionStatusMan.openPopDialog(\''+value[0].stationId+'\');';
		var pop = new Popup(lon,lat);
		pop.setTitle(value[0]['stationName']).setTitleIconClickListener(clicklistener).setTitleSelfClickListener(clicklistener);
		for(var i in value){
			var t = value[i]['eventStart'];
			t = new Date(t.time?t.time:t);
			var eventStart = (t.getMonth()+1) + '月' + t.getDate() + '日  ' + t.getHours() + '时' + t.getMinutes() + '分';
			var c = '<span style="margin-right:5px;">'+value[i]['protectName']+'</span><span style="color:red;margin-right:5px;">'+eventStart+'</span>' + value[i]['alarmMsg'];
			pop.setRowContent(c).setRowClickListener(clicklistener);
		}
		pop.setAutoSize(true);
		pop.showCloseIcon(true);
		return pop;
	}
}